package com.cb.domain.dao.user.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.cb.core.dao.hibernate.GenericDAOHibernate;
import com.cb.domain.dao.user.UserDAO;
import com.cb.domain.model.user.UserAccount;

@Repository
public class UserDAOImpl extends GenericDAOHibernate<UserAccount, Long> implements
		UserDAO {
	public UserDAOImpl() {
		super(UserAccount.class);
	}

	public UserAccount findByUserName(String userName) {
		String queryString = "from UserAccount where username=?";
		return (UserAccount) findUnique(queryString, new Object[] { userName });
	}

	public List<UserAccount> findByEmail(String email) {
		String queryString = "from UserAccount ua where ua.email=?";
		return find(queryString, new Object[] { email });
	}

	public Boolean isRegisterEmail(String email) {
		String queryString = "select userId from UserAccount ua where ua.email=?";
		List result = find(queryString, new Object[] { email });
		if (result.size() != 0)
			return true;
		return false;
	}

	public Boolean isRegisteredUserName(String userName) {
		String hql = "select userId from UserAccount u where u.username=?";
		List result = find(hql, new Object[] { userName });
		if (result.size() != 0)
			return true;
		return false;
	}

	public List<UserAccount> getAllUserType(Long type) {
		String queryString = " from UserAccount ua where ua.userLevel.userLevelStrategy.userLevelStrategyId=?";
		List<UserAccount> result = find(queryString, new Object[] { type });
		return result;
	}

	/**
	 * 获取全部用户数量
	 */
	public Integer getAllUserCount() {
		// TODO Auto-generated method stub
		String str = "select count(*) as total from User";
		List objs = this.find(str, new Object[] {});
		Object count = (Object) objs.get(0);
		Integer allCount = 0;
		if (null != count) {
			allCount = Integer.valueOf(count.toString());
		}
		return allCount;
	}

	public List getAllUserId(){
		String sql="select userId from User";
		return this.find(sql, new Object[]{});
	}
}
